package cn.edu.neusoft.wuguangsheng.flowerimpression.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.Utils.TimeUtils;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.OrderInfoActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.ShopMyCommentActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.activity.UpdateMyOrderCommentActivity;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.GoodsCommentBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.DeleteMyOrderCommentModel;

public class ShopMyCommentAdapter extends BaseAdapter<GoodsCommentBean> {

    String times;

    public ShopMyCommentAdapter(Context context, List<GoodsCommentBean> items, int layoutResourse) {
        super(context, items, layoutResourse);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(context).inflate(layoutResourse,parent,false);
        ShopMyCommentViewHolder viewHolder = new ShopMyCommentViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ShopMyCommentViewHolder viewHolder=(ShopMyCommentViewHolder) holder;
        GoodsCommentBean goodsCommentBean=items.get(position);
        final String number = goodsCommentBean.getOrder_number();
        final String content = goodsCommentBean.getOrc_content();
        final String time = goodsCommentBean.getOrc_time();
        final String orcid = goodsCommentBean.getOrc_id();
        final String orderid = goodsCommentBean.getOrder_id();

        viewHolder.myordercomment_number.setText(number);
        viewHolder.myordercomment_content.setText(content);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            times = TimeUtils.getTimeFormatText(date);
        }catch (Exception e){
            e.printStackTrace();
        }
        viewHolder.myordercomment_time.setText(times);
        viewHolder.myordercomment_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(context, OrderInfoActivity.class);
                intent1.putExtra("orderid",orderid);
                context.startActivity(intent1);

            }
        });

        viewHolder.myordercomment_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, UpdateMyOrderCommentActivity.class);
                intent.putExtra("orcid",orcid);
                intent.putExtra("content",content);
                context.startActivity(intent);
            }
        });
        viewHolder.myordercomment_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog alertDialog=new AlertDialog.Builder(context)
                        .setIcon(R.drawable.video_icon)
                        .setTitle("温馨提示")
                        .setMessage("您确认要删除评论吗？")
                        .setNegativeButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                DeleteMyOrderCommentModel deleteMyOrderComment = new DeleteMyOrderCommentModel();
                                deleteMyOrderComment.deleteMyOrderComment(orcid, new TListener<SuccessBean>() {
                                    @Override
                                    public void onResponse(SuccessBean successBean) {
                                        if (successBean.getSuccess().equals("1")){
                                            Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
                                            if(ShopMyCommentActivity.class.isInstance(context)){
                                                ShopMyCommentActivity activity = (ShopMyCommentActivity)context;
                                                activity.finish();}
                                        }
                                        else {
                                            Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    @Override
                                    public void onFail(String msg) {
                                        Toast.makeText(context,"网络错误",Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        })
                        .setPositiveButton("取消",null)
                        .create();
                alertDialog.show();
            }
        });
    }


}
